package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import model.Fox;
import model.Simulator;
import model.Redfox;
import model.Rabbit;

/**
 * Class Configview
 * @author Team X
 * @version 1
 */
@SuppressWarnings("unused")
public class ConfigController extends JFrame implements ActionListener 
{
	private static final long serialVersionUID = 1L;
	
	private JPanel panel;
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JMenuItem mntmFox;
	private JMenuItem mntmRabbit;
	private JMenuItem mntmRedFox;
	private JButton btnSubmitFox; 
	private JButton btnSubmitRabbit;
	private JButton btnSubmitRedFox;
	private JButton btnResetRabbit;
	private JButton btnResetRedFox;
	private JButton btnResetFox;
	private Simulator simulator;
	
    /** Create the frame. */
	public ConfigController(Simulator simulator) 
	{
		// AbstractView
		this.simulator = simulator;
		
		// Menubar
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnDieren = new JMenu("Dieren");
		menuBar.add(mnDieren);
		
		mntmRabbit = new JMenuItem("Rabbit");
		mntmRabbit.addActionListener(this);
		mnDieren.add(mntmRabbit);
		
	    mntmFox = new JMenuItem("Fox");
		mntmFox.addActionListener(this);
		mnDieren.add(mntmFox);
		
		mntmRedFox = new JMenuItem("RedFox");
	    mntmRedFox.addActionListener(this);
		mnDieren.add(mntmRedFox);
		
		JMenu mnInfo = new JMenu("Info");
		menuBar.add(mnInfo);
		
		// Create a contentpane
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
	}
	
	/** Create a view for the Fox configuration */
	private void createViewFox()
	{
		// Create a contentpane
		panel = new JPanel();
		panel.setBounds(0, 0, 457, 260);
		contentPane.add(panel);
		panel.setLayout(null);
		
		// Textfields fox configuration
		
		textField = new JTextField(Integer.toString(Fox.getCharacterInfo()[0]));
		textField.setBounds(164, 32, 195, 20);
		panel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField(Integer.toString(Fox.getCharacterInfo()[1]));
		textField_1.setColumns(10);
		textField_1.setBounds(164, 84, 195, 20);
		panel.add(textField_1);
		
		textField_2 = new JTextField(Double.toString(Fox.getBreedingProbability()));
		textField_2.setColumns(10);
		textField_2.setBounds(164, 136, 195, 20);
		panel.add(textField_2);
		
		textField_3 = new JTextField(Integer.toString(Fox.getCharacterInfo()[2]));
		textField_3.setColumns(10);
		textField_3.setBounds(164, 188, 195, 20);
		panel.add(textField_3);
		
		// Labels fox configuration
		
		JLabel lbltest = new JLabel("FOX");
		lbltest.setBounds(15, 10, 145, 14);
		panel.add(lbltest);
			
		JLabel lblVoortplantingsLeeftijd = new JLabel("Voortplantings leeftijd");
		lblVoortplantingsLeeftijd.setBounds(20, 37, 145, 14);
		panel.add(lblVoortplantingsLeeftijd);
			
		JLabel lblMaximaleLeeftijd = new JLabel("Maximale leeftijd ");
		lblMaximaleLeeftijd.setBounds(20, 88, 145, 14);
		panel.add(lblMaximaleLeeftijd);
			
		JLabel lblVoortplantingKans = new JLabel("Voortplanting kans");
		lblVoortplantingKans.setBounds(20, 139, 145, 14);
		panel.add(lblVoortplantingKans);
			
		JLabel lblMaximaleGeboortes = new JLabel("Maximale geboortes");
		lblMaximaleGeboortes.setBounds(20, 190, 145, 14);
		panel.add(lblMaximaleGeboortes);
			
		// Reset button
		btnResetFox = new JButton("Reset");
		btnResetFox.addActionListener(this);
		btnResetFox.setBounds(275, 237, 89, 23);
		panel.add(btnResetFox);
			
		// Save button
		btnSubmitFox = new JButton("Save");
		btnSubmitFox.addActionListener(this);
		btnSubmitFox.setBounds(368, 237, 89, 23);
		panel.add(btnSubmitFox);
	}
		
	/** Create a view for a rabbit */
	private void createViewRabbit()
	{
		// Panel
		panel = new JPanel();
		panel.setBounds(0, 0, 457, 260);
		contentPane.add(panel);
		panel.setLayout(null);
			
		// Textfields rabbit configuration 
		
		textField = new JTextField(Integer.toString(Rabbit.getCharacterInfo()[0]));
		textField.setBounds(164, 32, 195, 20);
		panel.add(textField);
		textField.setColumns(10);
			
		textField_1 = new JTextField(Integer.toString(Rabbit.getCharacterInfo()[1]));
		textField_1.setColumns(10);
		textField_1.setBounds(164, 84, 195, 20);
		panel.add(textField_1);
			
		textField_2 = new JTextField(Double.toString(Rabbit.getBreedingProbability()));
		textField_2.setColumns(10);
		textField_2.setBounds(164, 136, 195, 20);
		panel.add(textField_2);
			
		textField_3 = new JTextField(Integer.toString(Rabbit.getCharacterInfo()[2]));
		textField_3.setColumns(10);
		textField_3.setBounds(164, 188, 195, 20);
		panel.add(textField_3);
			
		// Labels rabbit configuration
		
		JLabel lbltest = new JLabel("RABBIT");
		lbltest.setBounds(15, 10, 145, 14);
		panel.add(lbltest);
			
		JLabel lblVoortplantingsLeeftijd = new JLabel("Voortplantings leeftijd");
		lblVoortplantingsLeeftijd.setBounds(20, 37, 145, 14);
		panel.add(lblVoortplantingsLeeftijd);
			
		JLabel lblMaximaleLeeftijd = new JLabel("Maximale leeftijd ");
		lblMaximaleLeeftijd.setBounds(20, 88, 145, 14);
		panel.add(lblMaximaleLeeftijd);
			
		JLabel lblVoortplantingKans = new JLabel("Voortplanting kans");
		lblVoortplantingKans.setBounds(20, 139, 145, 14);
		panel.add(lblVoortplantingKans);
			
		JLabel lblMaximaleGeboortes = new JLabel("Maximale geboortes");
		lblMaximaleGeboortes.setBounds(20, 190, 145, 14);
		panel.add(lblMaximaleGeboortes);
			
		// Submit button
		
		btnSubmitRabbit = new JButton("Oke");
		btnSubmitRabbit.addActionListener(this);
		btnSubmitRabbit.setBounds(368, 237, 89, 23);
		panel.add(btnSubmitRabbit);
			
		// Reset button
		
		btnResetRabbit = new JButton("Reset");
		btnResetRabbit.addActionListener(this);
		btnResetRabbit.setBounds(275, 237, 89, 23);
		panel.add(btnResetRabbit);
	}

	/** Create a view for a Redfox */
	private void createViewRedFox()
	{
		// Create contentpane
		panel = new JPanel();
		panel.setBounds(0, 0, 457, 260);
		contentPane.add(panel);
		panel.setLayout(null);
			
		// Textfields fox
		
		textField = new JTextField(Integer.toString(Redfox.getCharacterInfo()[0]));
		textField.setBounds(164, 32, 195, 20);
		panel.add(textField);
		textField.setColumns(10);
			
		textField_1 = new JTextField(Integer.toString(Redfox.getCharacterInfo()[1]));
		textField_1.setColumns(10);
		textField_1.setBounds(164, 84, 195, 20);
		panel.add(textField_1);
			
		textField_2 = new JTextField(Double.toString(Redfox.getBreedingProbability()));
		textField_2.setColumns(10);
		textField_2.setBounds(164, 136, 195, 20);
		panel.add(textField_2);
			
		textField_3 = new JTextField(Integer.toString(Redfox.getCharacterInfo()[2]));
		textField_3.setColumns(10);
		textField_3.setBounds(164, 188, 195, 20);
		panel.add(textField_3);
			
		// Label redfoxes
		
		JLabel lbltest = new JLabel("REDFOX");
		lbltest.setBounds(15, 10, 145, 14);
		panel.add(lbltest);
		
		JLabel lblVoortplantingsLeeftijd = new JLabel("Voortplantings leeftijd");
		lblVoortplantingsLeeftijd.setBounds(20, 37, 145, 14);
		panel.add(lblVoortplantingsLeeftijd);
			
		JLabel lblMaximaleLeeftijd = new JLabel("Maximale leeftijd ");
		lblMaximaleLeeftijd.setBounds(20, 88, 145, 14);
		panel.add(lblMaximaleLeeftijd);
			
		JLabel lblVoortplantingKans = new JLabel("Voortplanting kans");
		lblVoortplantingKans.setBounds(20, 139, 145, 14);
		panel.add(lblVoortplantingKans);
			
		JLabel lblMaximaleGeboortes = new JLabel("Maximale geboortes");
		lblMaximaleGeboortes.setBounds(20, 190, 145, 14);
		panel.add(lblMaximaleGeboortes);
			
		// Reset button
		
		btnResetRedFox = new JButton("Reset");
		btnResetRedFox.addActionListener(this);
		btnResetRedFox.setBounds(275, 237, 89, 23);
		panel.add(btnResetRedFox);
			
		// Submit button
		
		btnSubmitRedFox = new JButton("Oke");
		btnSubmitRedFox.addActionListener(this);
		btnSubmitRedFox.setBounds(368, 237, 89, 23);
		panel.add(btnSubmitRedFox);
	}

	/**
	 * Action handler
	 * @param ActionEvent action
	 */
	@Override
	public void actionPerformed(ActionEvent action) {
		if(action.getSource().equals(mntmFox))
		{
			if(panel != null) this.remove(panel);
			createViewFox();
			this.repaint();
			
		}
		if(action.getSource().equals(btnSubmitFox))
		{
			// Make the changes in the program
    		if (isInt(textField.getText())) Fox.changeCharacteristics("breeding_age", Integer.parseInt(textField.getText()));
    		if (isInt(textField_1.getText())) Fox.changeCharacteristics("max_age", Integer.parseInt(textField_1.getText()));
    		if (isDouble(textField_2.getText())) Fox.changeCharacteristics("breeding_probability", Double.parseDouble(textField_2.getText()));
    		if (isInt(textField_3.getText())) Fox.changeCharacteristics("max_litter_size", Integer.parseInt(textField_3.getText()));
   			
    		// Close the popup-window
    		this.remove(panel);
    		this.repaint();
    	}
			
		if(action.getSource().equals(btnResetFox))
		{
			Fox.defReset();
			this.remove(panel);
    		this.repaint();
		}
		if(action.getSource().equals(mntmRabbit))
		{
			if(panel != null) this.remove(panel);
			createViewRabbit();
			this.repaint();
		}
		if(action.getSource().equals(btnSubmitRabbit))
		{
			// Make the changes in the program
    		if (isInt(textField.getText())) Rabbit.changeCharacteristics("breeding_age", Integer.parseInt(textField.getText()));
    		if (isInt(textField_1.getText())) Rabbit.changeCharacteristics("max_age", Integer.parseInt(textField_1.getText()));
    		if (isDouble(textField_2.getText())) Rabbit.changeCharacteristics("breeding_probability", Double.parseDouble(textField_2.getText()));
    		if (isInt(textField_3.getText())) Rabbit.changeCharacteristics("max_litter_size", Integer.parseInt(textField_3.getText()));
   			
    		// Close the popup-window
    		this.remove(panel);
    		this.repaint();
    	}
		if(action.getSource().equals(btnResetRabbit))
		{
			Rabbit.defReset();
			this.remove(panel);
    		this.repaint();
		}
		if(action.getSource().equals(mntmRedFox))
		{
			if(panel != null) this.remove(panel);
			createViewRedFox();
			this.repaint();
				
		}
		if(action.getSource().equals(btnSubmitRedFox))
		{
			// Make the changes in the program
    		if (isInt(textField.getText())) Redfox.changeCharacteristics("breeding_age", Integer.parseInt(textField.getText()));
    		if (isInt(textField_1.getText())) Redfox.changeCharacteristics("max_age", Integer.parseInt(textField_1.getText()));
    		if (isDouble(textField_2.getText())) Redfox.changeCharacteristics("breeding_probability", Double.parseDouble(textField_2.getText()));
    		if (isInt(textField_3.getText())) Redfox.changeCharacteristics("max_litter_size", Integer.parseInt(textField_3.getText()));
   			
    		// Close the popup-window
    		this.remove(panel);
    		this.repaint();
    	}
		if(action.getSource().equals(btnResetRedFox))
		{
			Redfox.defReset();
			this.remove(panel);
    		this.repaint();
		}
	}	
	
	/**
	 * Int checker
	 * @param input
	 * @return boolean
	 */
	public boolean isInt(String input) 
	{
		try 
		{
		   Integer.parseInt(input);
		   return true;
		}
		catch(Exception e) 
		{
		      return false;
		}
	}	
	
	/**
	 * Double checker
	 * @param input
	 * @return boolean
	 */
	public boolean isDouble(String input) 
	{
		try 
		{
		   Double.parseDouble(input);
		   return true;
		}
		catch(Exception e) 
		{
		   return false;
		}
	}
}